<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/18 0018
 * Time: 15:30
 */

namespace Admin\Controller;

use Think\Controller;
class UserController extends Controller
{

    public function index(){
        $id=session('id');
        $adminmodel=M('admin');
        $admin=$adminmodel->where(array('id'=>$id))->select();
        $this->admin=$admin;

        $usersmodel=M('users');
        $users=$usersmodel->order('uid ')->select();
        $this->users=$users;
        /* dump($users);*/
        for ($i = 0; $i < count($users); $i++){
            $users[$i]["code"] = $i + 1;
        }
        if(!$id)
        {$this->error('登录已过期!',U("index.php/Admin/Index/index"));}
        elseif($users){
            $this->assign('list', $users);
            $this->display();
        }
        elseif ($admin){
            $this->assign('admin', $admin);
            $this->display();
        }
        else{ $this->error('获取失败!', U("Admin/User/userlist"));}

    }


    public function del(){

        $uid = $_GET['id'];
        $users = M("users");
        $users->uid = $uid;
        $users->delete();

        if ($users) {
            $this->success('删除用户成功');
        } else {
            $this->error('删除用户失败!');

        }

    }



    public function insert(){
        $data['username'] = trim(I('post.username'));
        $data['password'] = trim(I('post.password'));
        $data['sex'] = trim(I('post.sex'));
        $data['birthday'] = trim(I('post.birthday'));
        $data['qq'] = trim(I('post.qq'));
        $data['tel'] = trim(I('post.tel'));
        $data['intro'] = trim(I('post.intro'));
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/images/pic/'; // 设置附件上传根目录
        $upload->autoSub = false;
        $info = $upload->uploadOne($_FILES['pic']);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }
        $data['pic']=$info['savepath'].$info['savename'];
        $result=M('users')->where(array('username'=>$data['username']))->find();
        if($result)
            $this->error('用户名重复！', U("User/edituser"));
        else{
            M('users')->add($data);
            //var_dump($data);
            $url=U('User/index');
            header("Location: $url");
            exit(0);
        }

    }


   /* public function add()
    {

        $data['sname'] = trim(I('post.sname'));
        $data['password'] = md5(trim(I('post.password')));
        $data['gender'] = I('post.gender');
        $data['birthdate'] = I('post.birthdate');
        $data['degree'] = I('post.degree');
        $data['intro'] = I('post.intro');
        //echo $name;
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Upload/'; // 设置附件上传根目录
        //// 上传单个文件
        $info = $upload->uploadOne($_FILES['pic']);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }
        $data['pic']=$info['savepath'].$info['savename'];
        $result=M('student')->where(array('sname'=>$data['sname']))->find();
        if($result)
            $this->error('用户名重复！', U("Register/index"));
        else{
            M('student')->add($data);
            //var_dump($data);
            $url=U('Index/index');
            header("Location: $url");
            exit(0);
        }
    }*/




    public function editpswd($id){
         $usersmodel=M('users');
         $usersmodel->password="123456";
         if(false!==$usersmodel->where("uid=$id")->save()){
             $this->success('密码重置成功');
         }else{
             $this->error('密码重置失败');
         }
     }

public function search(){
    $username = trim(I('post.username'));
    $usersmodel=M('users');
    $a['username']=array('like',"%{$username}%");
    $select=$usersmodel->where($a)->select();

    for ($i = 0; $i < count($select); $i++){
        $select[$i]["code"] = $i + 1;
    }
  if($select)
  {
      $this->assign('list', $select);
      $this->display();
  }
else{ $this->error('未查到相关结果,请重新查询',U("User/index"));}


}




}